package com.wangrui.us3;

import cn.ucloud.ufile.UfileClient;
import cn.ucloud.ufile.api.object.ObjectConfig;
import cn.ucloud.ufile.auth.UfileAuthorizationException;
import cn.ucloud.ufile.auth.sign.UfileSignatureException;
import cn.ucloud.ufile.exception.UfileClientException;
import cn.ucloud.ufile.exception.UfileParamException;

public class SignedUrl {
    // 对象操作需要ObjectConfig来配置您的地区和域名后缀
    static ObjectConfig config = new ObjectConfig("cn-wlcb", "ufileos.com");
    public static void main(String[] args) {
        getPrivateURL("4741e49ac6e44b708fb41a31b8ffc368", "test-gboss-kj",60);
    }
    public static void getPublicURL(String keyName, String bucketName) {
        try {
        /*
        生成公共下载URL
        */
            String publicUrl = UfileClient.object(Constants.OBJECT_AUTHORIZER, config)
                    .getDownloadUrlFromPublicBucket(keyName, bucketName)
                    .createUrl();
            System.out.println(publicUrl);
        } catch (UfileParamException e) {
            e.printStackTrace();
        } catch (UfileAuthorizationException e) {
            e.printStackTrace();
        } catch (UfileSignatureException e) {
            e.printStackTrace();
        } catch (UfileClientException e) {
            e.printStackTrace();
        }
    }

    public static void getPrivateURL(String keyName, String bucketName, int expiresDuration) {
        try {
        /*
        生成私有下载URL
        */
            //  expiresDuration 私有下载路径的有效时间，即：生成的下载URL会在 创建时刻的时间戳 + expiresDuration秒后的时刻过期
            String publicUrl = UfileClient.object(Constants.OBJECT_AUTHORIZER, config)
                    .getDownloadUrlFromPrivateBucket(keyName, bucketName, expiresDuration)
                    .createUrl();
            System.out.println(publicUrl);
        } catch (UfileParamException e) {
            e.printStackTrace();
        } catch (UfileAuthorizationException e) {
            e.printStackTrace();
        } catch (UfileSignatureException e) {
            e.printStackTrace();
        } catch (UfileClientException e) {
            e.printStackTrace();
        }
    }
}
